<?php
/**
 * 销售管理
 * @author shitou
 *
 */
class SellAction extends CommonAction{
	
	/**
	 * 消费收银首页
	 */
	public function index(){
		
		createSession();
		
		if(IS_AJAX){
			
			$map['typeid']   = $this->_param('typeid','intval',0);
			$map['brandid']  = $this->_param('brandid','intval',0);
			$map['initials'] = $this->_param('initials','trim','');
			
			$json = sendRest("PMS/Product/proList",$map);
			$this->ajaxReturn($json['data']);
		}else{
			
			//购物车列表
			$view = $this->listCart();
			$this->assign("listCart",$view);
			
			//会员信息
			$map  = getMemberSession();
			$data = D("Sell")->readCard($map['vipno']);
			$this->assign("member",$data['data']);
			
			//商品列表
			$json = sendRest("PMS/Product/proList");
			$this->assign("proList",$json['data']);
			
			$this->display();
		}
	}
	
	/**
	 * 购物车列表
	 */
	public function listCart(){
		
		$map  = getMemberSession();
		$view = sendRest("OMS/Cart/index",$map);
		
		foreach($view['data'] as $k=>$v){
			$view['totalPrice'] += $v['amount']*$v['price'];
			$view['totalNum']   += $v['amount']; 
		}
		
		$view['totalPrice'] = number_format($view['totalPrice'],2);
		
		return $view;
	}
	
	/**
	 * 显示计费规则，选择商品数量
	 */
	public function detail(){
		
		$map['id'] = $this->_param("id","intval",0);
		$json      = sendRest("PMS/Product/detail",$map);
		$detail    = $json['data'];
		
		//获取计费类型
		$billing = C('BILLING_TYPE');
		
		//计费规则
		if($json['data']['attr']){
			
			$attr = json_decode($json['data']['attr'],true);
			foreach($attr as $k=>$v){
		
				$title = $billing[$detail['typeid']][$detail['unit']];
				$attr[$k]['jifeiTitle'] = $v['cnum'].$title.'/'.$v['sprice'].'元';
			}
			$this->assign("attr",$attr);
		}
		
		$this->assign("detail",$detail);
		$this->display();
	}
	
	/**
	 * 添加到购物车
	 */
	public function addCart(){
		
		$map = getMemberSession();
		
		$map['pid']  = $this->_param("pid","intval",0);
		$map['num']  = $this->_param("num","intval",1);
		$map['size'] = $this->_param("size","intval",1);
		
		$json = sendRest("OMS/Cart/add",$map);
		$this->ajaxReturn($json);
	}
	
	/**
	 * 删除购物商品
	 */
	public function delCart(){
		
		$map = getMemberSession();
		
		$map['id'] = $this->_param("id","intval",0);
		$json = sendRest("OMS/Cart/del",$map);
		
		$this->ajaxReturn($json);
	}
	
	/**
	 * 更新购物商品数量
	 */
	public function numCart(){
		
		
	}
	
	/**
	 * 清空购物车
	 */
	public function removeCart(){
		
		$map = getMemberSession();
		$json = sendRest("OMS/Cart/remove",$map); //清除该会员购物车
		
		if($json['status']==1){
			clearSession();
		}
	}
	
	/**
	 * 挂单
	 */
	public function lockOrder(){
		
		if(IS_AJAX){
			
			$map  = getMemberSession();
			$data = sendRest("OMS/Cart/index",$map);
			
			if($data['data']){
				$map['content'] = $this->_param("remark","trim","");
				$json = sendRest("OMS/Bill/lockOrder",$map);
				
				if($json['status']==1){
					$this->removeCart(); //清除该会员购物车
					$this->ajaxReturn($json);
				}else{
					$this->ajaxReturn(array("status"=>-1,"info"=>"挂单失败，请稍后重试"));
				}
			}else{
				$this->ajaxReturn(array("status"=>-1,"info"=>"请先添加商品到购物车"));
			}
		}else{
			$this->display();
		}
	}
	
	/**
	 * 解挂
	 */
	public function unlockOrder(){
		
		if(IS_AJAX){
			
			$map  = getMemberSession();
			$data = sendRest("OMS/Cart/index",$map); 
			
			if($data['data']){
				$this->ajaxReturn(array("status"=>-1,"info"=>"请清空购物车"));
			}else{
				
				$cat['billid'] = $this->_param("billid","intval",0);
				$json = sendRest("OMS/Bill/unlockOrder",$cat);
				
				if($json['status']==1){
					
					//转移购物车
					$cat['old']   = $json['data']['tableid'];
					$cat['oldid'] = $json['data']['dataid'];
					D("Sell")->transfer($cat);
					
					$this->ajaxReturn(array("status"=>1,"info"=>"解挂成功"));
				}else{
					$this->ajaxReturn(array("status"=>-1,"info"=>"解挂失败，请稍后重试"));
				};
			}
		}else{
			
			$json = sendRest("OMS/Bill/lockList");
			
			$this->assign("list",$json['data']);
			$this->display();
		}
	}
	
	/**
	 * 消费收银读卡
	 */
	public function sellReadCard(){
		
		$vipno = $this->_param("vipno","trim","");
		$json  = D("Sell")->readCard($vipno);
		$data  = $json['data'];
		
		//转移购物车
		if($json['status']==1){
			$map['oldid'] = $data['vipno'];
			$map['old']   = "Vipcard";
			D("Sell")->transfer($map);;
		}
		
		$this->ajaxReturn($json);
	}
	
	/**
	 * 添加订单
	 */
	public function addSellOrder(){
		
		$map  = getMemberSession();
		$data = sendRest("OMS/Order/add",$map);
		
		if($data['status']==1){
			$this->removeCart();
		}
		
		$this->ajaxReturn($data);
	}
	
	/**
	 * 结账
	 */
	public function payment(){
		
		if(IS_AJAX){
			
			$payment = $this->_param("payment","trim","");
			$cash    = $this->_param("payMoney");
			$bank    = 0;
			$other   = 0;
			
			if($payment=="Other"){
				$cash  = $this->_param("CashPay");
				$bank  = $this->_param("BankPay");
				$other = $this->_param("OtherPay");
			}
			
			$map['payment'] = array(
				"Cash"  => $cash,
				"Bank"  => $bank,
				"Other" => $other,
			);
			
			$map['oldid']   = $this->_param("oldid");
			$json = sendRest("OMS/Order/pay",$map);
			$this->ajaxReturn($json);
		}else{
			
			$map['orderno'] = $this->_param("orderno","trim","");
			//dump($map);exit;
			$json = sendRest("OMS/Order/payment",$map);
			
			$this->assign("detail",$json['data']);
			$this->display();
		}
	}
	
	/**
	 * 计次消费首页
	 */
	public function sellNum(){
		
		if(IS_AJAX){
			//读卡操作
			$vipno  = $this->_get("vipno","trim","");
			$map    = array("vipno"=>$vipno);
			$json   = sendRest("MMS/Custom/readCards", $map);
			if($json['data']['id']){
				
				setSession($vipno,"Vipcard");
			}
			$cus    = array("dataid"=>$json['data']['id']);
			$json_1 = sendRest("OMS/Sell/numberUser", $cus);
			$data   = array("sellInfo"=>$json_1['data'],"cusInfo"=>$json['data']);
			$this->ajaxReturn($data);
			
		}else{
			
			$this->display();
		}
	}
	
	/**
	 * 计次消费处理
	 */
	public function sellNumExpense(){	
		
		if(IS_AJAX){
			$ses = getMemberSession();
			//消费操作
			$id      = $this->_get("id",     "intval", 0);
			$num     = $this->_get("num",    "intval", 0);
			$content = $this->_get("content","trim",   "");
			$dataid  = $ses['dataid'];
			$tableid = $ses['tableid'];
			
			$param = array("id"=>$id, "num"=>$num,"dataid"=>$dataid,"tableid"=>$tableid,"content"=>$content);

			$json  = sendRest("OMS/Sell/numberExpense",  $param);
			
			$this->ajaxReturn($json);
			
			
		}else{
			
			$id   = $this->_get("id","intval",1);
			$map  = array("id"=>$id);
			$json = sendRest("OMS/Sell/numberDetail",$map);
			$this->assign("detail",$json['data']);
			$this->display();
		}
	}
	
	/**
	 * 计次消费日志
	 */
	public function numberLog(){
		
		$numid = $this->_get("numid","intval",1);
		$vipno = $this->_get("vipno","trim",  "");
		
		$map   = array("numid"=>$numid,"dataid"=>$vipno);
		$json  = sendRest("OMS/Sell/numberLog",$map);
		$this->assign("detail",$json['data']);
		
		$this->display();
	}
	
	/**
	 * 计时消费
	 */
	public function sellTime(){
	
		if(IS_AJAX){
			//读卡操作
			$vipno = $this->_param("vipno","trim","");
			$json  = D("Sell")->readCard($vipno);
			
			if($json['status']==1){
				setSession($json['data']['vipno'],"Vipcard");//重新设置session
			}
				
			$this->ajaxReturn($json);
		}else{
				
			//所有计时消费项目
			$proList  = D("Sell")->proList();
			$this->assign("proList",$proList['data']);
				
			//当前正在进行的计时消费项目
			$timeNow  = D("Sell")->timeNowItem();
			$this->assign("timeNow",$timeNow['data']);
			
			//dump($timeNow);exit;
				
			//客户拥有的计时消费项目
			$timeUser = D("Sell")->timeUser();
			$this->assign("timeUser",$timeUser['data']);
				
			$this->display();
		}
	}
	
	/**
	 * 添加计时项目
	 */
	public function timeAdd(){
		
		if(IS_AJAX){
			
			$map         = getMemberSession();
			$map['pid']  = $this->_param("id","intval",0);
			$map['rent'] = $this->_param("rent","intval",0);
			$map['num']  = $this->_param("amount","intval",0);
			$map['content'] = $this->_param("content","trim","");
			
			$json = sendRest("OMS/Sell/timeAdd",$map);
			
			if($json['status']==1){
				clearSession();//清除并重新设置session
			}
			
			$this->ajaxReturn($json);
		}else{
			$id = $this->_param("id","intval",0);
			
			//所有计时消费项目
			$proList  = D("Sell")->proList();
			$this->assign("proList",$proList['data']);
			
			//获取计费类型
			$billing = C('BILLING_TYPE');
			$timeArr = $billing['4'];
			$this->assign("timeArr",$timeArr);
			
			$this->assign("pid",$id);
			$this->display();
		}
	}
	
	/**
	 * 开始计时
	 * @access public 
	 * @param  Int    id     消费项目id
	 * @param  Int    rent   押金金额
	 * @param  Int    unit   计时单位
	 * @param  Int    amount 限时数量
	 * @param  Int    limit  是否限时，1是2否
	 */
	public function startTime(){

		$map['id']     = $this->_param("id","intval",0);
		$json = sendRest("OMS/Sell/timeBegin",$map);
		
		$this->ajaxReturn($json);
	}
	
	/**
	 * 结束计时
	 */
	public function endTime(){
	
		$map['id'] = $this->_param("id","intval",0);
		$json = sendRest("OMS/Sell/timeEnd",$map);
	
		$this->ajaxReturn($json);
	}
	
	/**
	 * 延时
	 * @access public
	 * @param  Int id   计时项目id
	 * @param  Int size 计时时间
	 * @param  Int unit 计时单位
	 */
	public function delayTime(){
	
		$map['id']   = $this->_param("id","intval",0);
		$map['size'] = $this->_param("size","intval",0);
		$map['unit'] = $this->_param("unit","intval",0);
	
		$json = sendRest("OMS/Sell/delayTime",$map);
		$this->ajaxReturn($json);
	}
	
	/**
	 * 取消计时
	 * @access public
	 * @param  Int id 计时项目id
	 */
	public function cancelTime(){
	
		$map['id']   = $this->_param("id","intval",0);
	
		$json = sendRest("OMS/Sell/cancelTime",$map);
		$this->ajaxReturn($json);
	}
}
?>